package org.testfire.support;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Sep 29, 2007
 * Time: 9:53:51 PM
 *
 */

public class PasswordDialog extends JDialog
{
    JPanel panel1 = new JPanel();

    BorderLayout borderLayout1 = new BorderLayout();

    JPanel jpSouth = new JPanel();

    JPanel jpCenter = new JPanel();

    JButton jbOk = new JButton();

    JButton jbCancel = new JButton();

    JLabel jlUserName = new JLabel();

    JTextField jtfUserName = new JTextField();

    JLabel jlPassword = new JLabel();

    JPasswordField jpfPassword = new JPasswordField();

    JLabel jlMessage = new JLabel();

    String defaultUserName = "";

    boolean userCanceled = false;

    public PasswordDialog( Frame frame, String title, String message )
    {
        super( frame, title, true );
        try
        {
            jbInit();
            String msg = "<html><center>" + message.replaceAll( "\\n", "<br>" ) + "</center></html>";
            this.jlMessage.setText( msg );
            CenterComponent( frame, this );
            pack();
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }
    }

    public void setVisible( boolean val )
    {
        super.setVisible( val );
    }

    public Object[] getUserInput()
    {
        super.setVisible( true );
        if ( this.userCanceled )
        {
            return null;
        }
        return new Object[]{jtfUserName.getText(), jpfPassword.getPassword()};
    }

    public void setDefaultUserName( String defaultUserName )
    {
        this.defaultUserName = defaultUserName;
        jtfUserName.setText(defaultUserName);
        jtfUserName.selectAll();
    }

    public String getUserName()
    {
        return jtfUserName.getText();
    }

    public char[] getPassword()
    {
        return jpfPassword.getPassword();
    }

    public boolean getUserCanceled(){
        return userCanceled;
    }

    public PasswordDialog()
    {
        this( null, "Please enter user name and password", "Please enter user name and password." );
    }

    private void jbInit() throws Exception
    {
        panel1.setLayout( borderLayout1 );
        this.setDefaultCloseOperation( javax.swing.WindowConstants.HIDE_ON_CLOSE );
        this.setFont( new java.awt.Font( "Dialog", 0, 12 ) );
        this.setModal( true );
        this.setResizable( false );
        this.setTitle( "Please enter user name and password." );
        jbOk.setText( "Okay" );
        jbOk.addActionListener( new java.awt.event.ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                jbOk_actionPerformed( e );
            }
        } );

        jbCancel.setText( "Cancel" );
        jbCancel.addActionListener( new java.awt.event.ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                jbCancel_actionPerformed( e );
            }
        } );
        jpCenter.setLayout( null );
        jlUserName.setText( "UserName:" );
        jlUserName.setBounds( new Rectangle( 35, 79, 80, 15 ) );
        jtfUserName.setText( this.defaultUserName );
        jtfUserName.setBounds( new Rectangle( 115, 76, 181, 20 ) );
        jtfUserName.addKeyListener( new java.awt.event.KeyAdapter()
        {
            public void keyReleased( KeyEvent e )
            {
                jtfUserName_keyReleased( e );
            }
        } );
        jlPassword.setText( "Password:" );
        jlPassword.setBounds( new Rectangle( 35, 113, 80, 15 ) );
        jlMessage.setFont( new java.awt.Font( "Dialog", 0, 11 ) );
        jlMessage.setHorizontalAlignment( SwingConstants.CENTER );
        jlMessage.setHorizontalTextPosition( SwingConstants.CENTER );
        jlMessage.setText( "Please enter your user name and password." );
        jlMessage.setVerticalAlignment( javax.swing.SwingConstants.CENTER );
        jlMessage.setBounds( new Rectangle( 21, 5, 312, 61 ) );
        panel1.setPreferredSize( new Dimension( 350, 180 ) );
        jpfPassword.addKeyListener( new java.awt.event.KeyAdapter()
        {
            public void keyReleased( KeyEvent e )
            {
                jpfPassword_keyReleased( e );
            }
        } );
        jpfPassword.setBounds( new Rectangle( 115, 107, 181, 22 ) );
        jpCenter.setPreferredSize( new Dimension( 350, 180 ) );
        getContentPane().add( panel1 );
        jpSouth.add( jbOk, null );
        jpSouth.add( jbCancel, null );
        panel1.add( jpCenter, BorderLayout.CENTER );
        jpCenter.add( jpfPassword, null );
        jpCenter.add( jlUserName, null );
        jpCenter.add( jlPassword, null );
        jpCenter.add( jlMessage, null );
        jpCenter.add( jtfUserName, null );
        panel1.add( jpSouth, BorderLayout.SOUTH );
    }

    void jbOk_actionPerformed( ActionEvent e )
    {
        this.dispose();
    }

    void jbCancel_actionPerformed( ActionEvent e )
    {
        this.userCanceled = true;
        this.dispose();
    }

    void jtfUserName_keyReleased( KeyEvent e )
    {
        if ( e.getKeyCode() == KeyEvent.VK_ENTER )
        {
            jpfPassword.requestFocus();
        }
    }

    void jpfPassword_keyReleased( KeyEvent e )
    {
        if ( e.getKeyCode() == KeyEvent.VK_ENTER )
        {
            jbOk_actionPerformed( null );
        }

    }

    public static void CenterComponent( Component comp )
    {
        CenterComponent( null, comp );
    }

    public static void CenterComponent( Component parent, Component comp )
    {
        Dimension dlgSize = comp.getPreferredSize();
        Dimension frmSize;
        Point loc;
        if ( parent != null )
        {
            frmSize = parent.getSize();
            loc = parent.getLocation();
        }
        else
        {
            frmSize = Toolkit.getDefaultToolkit().getScreenSize();
            loc = new Point( 0, 0 );
        }
        comp.setLocation( frmSize.width / 2 - dlgSize.width / 2 + loc.x,
            frmSize.height / 2 - dlgSize.height / 2 + loc.y );

    }

}
